package com.glory.dmegcpda.view

import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import com.glory.dmegcpda.R
import com.glory.dmegcpda.base.Constant
import com.glory.dmegcpda.databinding.ActivityKeepInfoBinding
import com.glory.mvvmnet.base.BaseActivity
import com.glory.dmegcpda.viewmodel.KeepInfoViewModel
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DateUtils
import kotlinx.android.synthetic.main.activity_keep_info.*
import kotlinx.android.synthetic.main.activity_title.*


/**
 * FileName: KeepInfoActivity
 * Author: XJH
 * Date: 2021/10/21 17:37
 * Description:
 * History:
 */
class KeepInfoActivity : BaseActivity<KeepInfoViewModel>(),
    View.OnClickListener {
    var sheetRRN : String? = null
    var objectRRN : String? = null
    var binding : ActivityKeepInfoBinding? = null
    override fun layoutId(): Int {
        return R.layout.activity_keep_info
    }

    private fun getText(editText: EditText) : String{
        return editText.text.toString().trim()
    }

    override fun initView(savedInstanceState: Bundle?) {
        binding = DataBindingUtil.setContentView(this,R.layout.activity_keep_info)
        binding!!.keepInfoViewModel = viewModel
        val extras = intent.extras
        sheetRRN = extras?.getString("SHEETRRN","")
        objectRRN = extras?.getString("OBJECTRRN", "")
        val eqp = extras?.getString("EQPID", "")
        val sheetName = extras?.getString("SHEETNAME", "")
        tvEqpID.text = eqp
        tvSheetName.text = sheetName
        tvCheckTime.text = FreeApi_DateUtils.getNowStringDate("yyyy/MM/dd HH:mm:ss")
        tvCheckPerson.setText(Constant.userName)
        tvTitle.text = "保养任务"
        tvKeep.setOnClickListener(this)
        tvPart.setOnClickListener(this)
        tvTemp.setOnClickListener(this)
        tvSave.setOnClickListener(this)

        viewModel.init(objectRRN!!)
        rvKeepList.adapter = viewModel.mKeepAdapter
        rvPartList.adapter = viewModel.mPartAdapter
        ivBack.setOnClickListener { finish() }
    }

    override fun initData() {
        viewModel.defUI.startActivity.observe(this){
            setResult(RESULT_OK)
            finish()
        }
    }

    override fun onClick(v: View?) {
        when(v?.id){
            R.id.tvKeep ->{
                setViewStates(false)
                llKeepLayout.visibility = View.VISIBLE
                llPartLayout.visibility = View.GONE
            }
            R.id.tvPart ->{
                setViewStates(true)
                llKeepLayout.visibility = View.GONE
                llPartLayout.visibility = View.VISIBLE
            }
            R.id.tvTemp ->{
                viewModel.save(objectRRN!!,getText(tvCheckPerson),"N")
            }
            R.id.tvSave ->{
                viewModel.save(objectRRN!!,getText(tvCheckPerson),"Y")
            }
        }
    }

    private fun setViewStates(states: Boolean) {
        tvPart.setBackgroundColor(
            if (states) resources.getColor(R.color.free_ui_blue_sky_theme_color) else resources.getColor(
                R.color.white
            )
        )
        tvPart.setTextColor(
            if (states) resources.getColor(R.color.white) else resources.getColor(
                R.color.black
            )
        )
        tvKeep.setBackgroundColor(
            if (states) resources.getColor(R.color.white) else resources.getColor(
                R.color.free_ui_blue_sky_theme_color
            )
        )
        tvKeep.setTextColor(
            if (states) resources.getColor(R.color.black) else resources.getColor(
                R.color.white
            )
        )
    }
}